from http.cookies import SimpleCookie

from bs4 import BeautifulSoup

from common.dbutils import DBUtils


# # 提取 JSESSIONID 值
# def extract_jsessionid(cookie_string):
#         if cookie_string:
#             cookies = SimpleCookie()
#             cookies.load(cookie_string)
#             if 'JSESSIONID' in cookies:
#                 return cookies['JSESSIONID'].value
#         return None

# # 提取完整格式的 JSESSIONID
# def extract_full_jsessionid(cookie_string):
#     if cookie_string:
#         cookies = SimpleCookie()
#         cookies.load(cookie_string)
#         if 'JSESSIONID' in cookies:
#             return f"JSESSIONID={cookies['JSESSIONID'].value}"
#     return None

# 第三方信息解析方法
def parse_third_info(html):
    # # 获取第三方的开户信息,使用BeautifulSoup解析HTML
    # html_data = open_account.open_account(session).json().get("description").get('form')
    # print(html_data)
    # html.parser:为HTML的解析器
    soup = BeautifulSoup(html, 'html.parser')
    # 获取form标签中method属性的值
    # print(soup.form['method'])
    # 获取form标签中action属性的值
    url = soup.form['action']
    # print(soup.form['action'])
    # 获取所有input标签中name属性的值以及value属性的值,并且保存在字典中
    json_data = {}
    for input_tag in soup.find_all('input'):
        json_data[input_tag['name']] = input_tag['value']
    # print(json_data)
    return url, json_data

# 断言
def assert_value(message,status_code,status,infor):
    assert message.status_code == status_code
    assert message.json().get('status') == status
    assert infor in message.text

# 处理手机号唯一性问题-数据库操作
def phone_unique(phone):
    # 处理手机号唯一性问题-数据库操作
    db = DBUtils()
    db.execute_sql(f"delete from mb_member where phone={phone};")
    db.execute_sql(f"delete from mb_member_info where member_name='{phone}';")
    db.execute_sql(f"delete from mb_member_login_log where member_name='{phone}';")
    db.execute_sql(f"delete from mb_member_register_log where phone={phone};")
